package com.epam.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.epam.DAO.CarDAO;
import com.epam.DAO.Factories.DAOFactory;
import com.epam.entity.Car;

public class Start extends HttpServlet {

	private static final long serialVersionUID = 1L;

	EntityManagerFactory emf;
	EntityManager em;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		resp.setContentType("text/html");
		PrintWriter out = resp.getWriter();

		// CarDAOImpl carDAOImpl = new CarDAOImpl();
		// Car car = new Car("AA", "BB", "CC", 1.2f, true, "D");
		// carDAOImpl.addCar(car);

		out.println("<HTML><HEAD>");
		out.println("<TITLE></TITLE>");
		out.println("</HEAD><BODY>");

//		BillDAO billDAO = new BillDAOEclipseLink();
//		List<Bill> bills = billDAO.getAllBills();
//		out.println("<br>");
//		out.println("<br>");
//		out.println("billDAO.getAllBills()");
//		for (Bill bill : bills) {
//			out.println("<br>");
//			out.println(bill);
//		}
//
//		bills = billDAO.getAllPayedBills();
//		out.println("<br>");
//		out.println("<br>");
//		out.println("billDAO.getAllPayedBills()");
//		for (Bill bill : bills) {
//			out.println("<br>");
//			out.println(bill);
//		}
//
//		bills = billDAO.getAllNotPayedBills();
//		out.println("<br>");
//		out.println("<br>");
//		out.println("billDAO.getAllNotPayedBills()");
//		for (Bill bill : bills) {
//			out.println("<br>");
//			out.println(bill);
//		}

		// bills = billDAOImpl.getUserBills(1);
		// out.println("<br>");
		// out.println("<br>");
		// out.println("billDAOImpl.getUserBills(1);");
		// for (Bill bill : bills) {
		// out.println("<br>");
		// out.println(bill);
		// }

//		UserDAO userDAO = new UserDAOEclipseLink();
//		// userDAO.addUser("QQQQ", "111");
//
//		User user;
//		out.println("<br>");
//		out.println("<br>");
//		out.println("userDAO.getUser(\"QQQ\", \"123\")");
//		out.println("<br>");
//		try {
//			user = userDAO.read("QQQ", "123");
//			out.println(user);
//		} catch (NoResultException nre) {
//			out.println("<br>");
//			out.println("nosuch user");
//			out.println("<br>");
//		}
//
//		// work
//		out.println("<br>");
//		out.println("<br>");
//		out.println("userDAO.getUser(\"QQQQ\", \"123\")");
//		out.println("<br>");
//		try {
//			user = userDAO.read("QQQQ", "123");
//			out.println(user);
//		} catch (NoResultException nre) {
//			out.println("nosuch user");
//			out.println("<br>");
//		}

		DAOFactory daoFactory = DAOFactory.getDAOFactory(DAOFactory.CURRENT_IMPLEMENTATION);
//		RequestDAO requestDAO = daoFactory.getRequestDAO();
//		List<Request> requests;
//		
//		requests= requestDAO.getUserClosedRequests(1);
//		out.println("<br>");
//		out.println("<br>");
//		out.println("requestDAO.getUserRequests(1)");
//		for (Request request : requests) {
//			out.println("<br>");
//			out.println(request);
//		}
		
//		ClientDAOEclipseLink clientDAO = new ClientDAOEclipseLink();
//		List<Client> clients;
//		
//		
//		clients = clientDAO.getClientRequestCancelled();
//		out.println("<br>");
//		out.println("<br>");
//		out.println("clientDAO.getClientRequestCancelled()");
//		for (Client client : clients) {
//			out.println("<br>");
//			out.println(client);
//		}
//		
		CarDAO carDAO = daoFactory.getCarDAO();
		List<Car> cars;
		
		cars = carDAO.getAvaliableCars();
		out.println("<br>");
		out.println("<br>");
		out.println("clientDAO.getClientRequestCancelled()");
		for (Car car : cars) {
			out.println("<br>");
			out.println(car);
		}
		
		
		
		out.println("<br>");
		out.println("<br>");
		out.println("bla-bla");
		out.println("</BODY></HTML>");
		out.close();

		// try {
		//
		// emf = Persistence.createEntityManagerFactory("CarRent2");
		// em = emf.createEntityManager();
		//
		// em.getTransaction().begin();
		// Car car = new Car("A", "B", "C", 1.2f, (byte)1, "D");
		//
		// em.persist(car);
		// em.getTransaction().commit();
		// // } catch (Exception e) {
		// // throw new ServletException(e);
		// } finally {
		// if (em != null) {
		// em.close();
		// }
		// }

	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

}
